package cn.pzhu.pserson.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.pzhu.pserson.dao.dao.UserinfoMapper;
import cn.pzhu.pserson.util.SqlUtil;
import cn.pzhu.pserson.model.Userinfo;
import cn.pzhu.pserson.service.IUserinfoService;

@Service
public class UserinfoService implements IUserinfoService {
    @Resource
    private UserinfoMapper userinfoMapper;

    @Override
    public boolean save(Userinfo userinfo) {
        return userinfoMapper.insert(userinfo) > 0;
    }

    @Override
    public boolean update(Userinfo userinfo) {
        return userinfoMapper.updateByPrimaryKeySelective(userinfo) >= 0;
    }

    @Override
    public boolean delete(String ip) {
        return userinfoMapper.deleteByPrimaryKey(ip) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return userinfoMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public Userinfo findModel(String ip) {
        return userinfoMapper.selectByPrimaryKey(ip);
    }

    @Override
    public Userinfo findModel(String ip, String[] parameters) {
        return userinfoMapper.findModel(ip, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String ip) {
        String[][] parameters = {{"ip", "ip"}, {"count", "count"}, {"ifshare", "ifshare"}, {"ifquan", "ifquan"}, {"attr1", "attr1"}, {"attr2", "attr2"}, {"attr3", "attr3"}, {"attr4", "attr4"}, {"attr5", "attr5"}, {"attr6", "attr6"}};
        return this.findMap(ip, parameters);
    }

    public Map<String, Object> findMap(String ip, String[][] parameters) {
        return userinfoMapper.findMap(ip, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<Userinfo> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<Userinfo> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Userinfo> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return userinfoMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"ip", "ip"}, {"count", "count"}, {"ifshare", "ifshare"}, {"ifquan", "ifquan"}, {"attr1", "attr1"}, {"attr2", "attr2"}, {"attr3", "attr3"}, {"attr4", "attr4"}, {"attr5", "attr5"}, {"attr6", "attr6"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"ip", "ip"}, {"count", "count"}, {"ifshare", "ifshare"}, {"ifquan", "ifquan"}, {"attr1", "attr1"}, {"attr2", "attr2"}, {"attr3", "attr3"}, {"attr4", "attr4"}, {"attr5", "attr5"}, {"attr6", "attr6"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return userinfoMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return userinfoMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }

}
